package com.miguo.utils;

import android.app.Activity;

import com.miguo.app.AppHelper;
import com.miguo.bean.GoodsBean;
import com.miguo.db.GoodsDB;
import com.miguo.framework.request.FailData;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.request.RequestJsonHandler;
import com.miguo.framework.scan.decode.CaptureActivityHandler;
import com.miguo.framework.utils.Toast;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.ui.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ScanUtils {
    private Activity mActivity;

    private static ScanUtils instance;

    private int StartResult;
    private int EndResult;

    private ScanUtils(Activity activity) {
        this.mActivity = activity;
    }

    public static ScanUtils getInstance(Activity activity) {
        if (instance == null) {
            instance = new ScanUtils(activity);
        }
        return instance;
    }

    public void getData(String url, final CaptureActivityHandler captureHandler) {
        RequestJsonHandler handler = new RequestJsonHandler() {
            @Override
            public void onRequestFinish(JsonData json) {

                if (json.optInt("status") == 200) {
                    List<GoodsBean> mGoods = new ArrayList<GoodsBean>();
                    JsonData data = json.optJson("data");
                    GoodsBean Data = new GoodsBean(data);
                    mGoods.add(Data);

                    if (mGoods != null && mGoods.size() > 0) {
                        GoodsDB Goods = GoodsDB.getInstance(mActivity);
                        if (Goods.isSave(mGoods.get(0).getItem_id(), 1)) {
                            Toast.show(mActivity, "该商品已收藏~!");
                            captureHandler.sendEmptyMessage(R.id.restart_preview);
                        } else {
                            Goods.save(mGoods.get(0));
                            StartResult = DateUtils.getStartTime(mGoods.get(0)
                                    .getBegin_time());
                            EndResult = DateUtils.getStartTime(mGoods.get(0)
                                    .getEnd_time());
                            if (StartResult == 2 && EndResult != 3) {
                                LocalNotiUtils.setLocalNoti(mActivity,
                                        mGoods.get(0));
                                Toast.show(mActivity, "收藏预告~系统将提前5分钟通知您~");
                            } else {
                                Toast.show(mActivity, "商品收藏完成了啦~");
                            }
                            AppHelper.StartScanFavor(mActivity);
                        }
                    } else {
                        Toast.show(mActivity, "暂无该商品信息~");
                        captureHandler.sendEmptyMessage(R.id.restart_preview);
                    }
                }else{
                    Toast.show(mActivity, json.optString("info"));
                    captureHandler.sendEmptyMessage(R.id.restart_preview);
                }
            }

            @Override
            public void onRequestFail(FailData data) {
                Toast.show(mActivity, "查询失败~查看网络是否正常~");
                captureHandler.sendEmptyMessage(R.id.restart_preview);
            }
        };
        Map<String, String> param = new HashMap<String, String>();
        param.put("url", url);
        HttpClient.post(Url.toRoute(Url.Scan.Item, 1), param, handler);
    }
}
